// pages/content/payment.js
var app = getApp();
var rsa = require('../../utils/rsa.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputnumber: '',
    items: [
      { name: '支付宝', value: '支付宝' },
      { name: '微信', value: '微信', checked: 'true' },
      { name: '银联', value: '银联' },
      { name: '现金', value: '现金' },
    ],
    goodList: [],  // var good = {goodbtn:'',num:1,name:'',price:0.00};
    huyflag:false,

    inpttext:'',   //会员到店输入
    vipArr_title:'',  //会员到店提示
    vipdetail:'',  //vip详情
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const tel = options.tel;
    if(tel){
      this.viprequest(tel);
    }
  },
  //会员到店
  viparrive:function(){
    this.setData({
      huyflag: true,
    })
  },
  hidedaod:function(){
    this.setData({
      huyflag: false,
      vipArr_title: '',
    })
  },
  //确定
  userArrive:function(){
    const tel = this.data.inpttext;
    if (!tel) {
      this.setData({
        vipArr_title: '手机号不为空',
      })
    } else {
      this.setData({
        vipArr_title: '处理中...',
      })
      this.viprequest(tel);
     
    }
  },
  viprequest:function(e){
    var shopnum = app.globalData.shoopnum;
    const setion = app.globalData.session;
    wx.showLoading({
      title: '请求中...',
      mask: true,
    })
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'promotion.member_regist', "seller_bn": shopnum, "session_id": setion, "mobile": e
    }, (err, res) => {
      wx.hideLoading();
      if (err) {
        this.setData({
          vipArr_title: err,
        })
        return;
      }
      this.setData({
        vipdetail: res.data, //vip详情
        vipArr_title: '',
        huyflag: false,
        inputnumber: '',
        goodList: [],
      })
    })
  },
  inpaction:function(e){
    this.setData({
      inpttext:e.detail.value,
      vipArr_title:'',
    })
  },
  //查询商品
  requestGoodsDetail: function (e) {
    var that = this;
    var templist = this.data.goodList;
    const shopnum = app.globalData.shoopnum;
    const settion = app.globalData.session;
    var mp = that.data.inputnumber ? that.data.inputnumber : '0';
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'goods.search', "seller_bn": shopnum, 'session_id': settion, "cat_id": '', "name": '', "syns_time": '', "barcode": e, "page_num": '10', "page": '1'
    }, (err, res) => {
      if (err) {
        wx.showModal({
          title: '错误',
          content: err,
          showCancel:false,
        })
        return
      }

      var redata = res.data.list;
      if (redata.length > 0) {
        var good = {goodbtn:'',num:1,name:'',price:0.00};
        var regood = redata[0];
        var kuc = regood.quantity;
        if (parseInt(kuc)<1){
          wx.showModal({
            title: '提示',
            content: '库存不足',
            showCancel: false,
          })
          return;
        }
        good.goodbtn = regood.bn;
        good.name = regood.name;
        var vipprices = regood.member_lv_price;  //会员价格数组
        var vipcode = this.data.vipdetail.member_lv_id;  //会员ID
        if (vipcode && vipprices.length>0){
          for(var i= 0 ;i<vipprices.length;i++){
            var content_price = vipprices[i];
            if (vipcode == content_price.member_lv_id){
              good.price = content_price.price;
              break;
            }else{
              good.price = regood.price;
            }
          }
        }else{
          good.price = regood.price;
        }
        templist.unshift(good);

        var money = good.price;
        var amont = parseFloat(mp) + parseFloat(money);
        that.setData({
          goodList: templist,
          inputnumber: amont.toString()
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '未查询到该商品',
          showCancel: false,
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //键盘组建
  keynumberAction: function (e) {
    var oragin = this.data.inputnumber;
    const current = e.currentTarget.dataset.info;
    var nowp = oragin + current.toString();
    this.setData({
      inputnumber: nowp,
    })
  },
  scanAction: function () {
    var that = this;
    var goods = that.data.goodList;
    var mp = that.data.inputnumber ? that.data.inputnumber : '0';
    wx.scanCode({
      success: (res) => {
        this.requestGoodsDetail(res.result);
      }
    })
  },
  //delete  
  reduceAction: function () {
    var oragin = this.data.inputnumber;
    var temp = oragin.substring(0, oragin.length - 1);
    this.setData({
      inputnumber: temp,
    })
  },
  reducegoodsAction: function (e) {
    var mp = this.data.inputnumber ? this.data.inputnumber : '0';
    const index = e.currentTarget.dataset.info;
    var goods = this.data.goodList;
    var cuen = goods.splice(index, 1);
    var money = cuen[0].price;
    var temp = parseFloat(mp) - parseFloat(money);
    var amont = temp < 0 ? 0 : temp;
    this.setData({
      goodList: goods,
      inputnumber: amont.toString(),
    })
  },
  //下单
  payAction: function () {
    const cost = this.data.inputnumber;
    
    var cvl = parseFloat(cost);
    if (cvl <= 0 || cost == '') {
      wx.showToast({
        title: '提现金额不为0',
        icon:'none',
        duration:3000
      })
      return
    }
    
      this.fastorder();
    
  },

  //下单
  orderwithgoods: function () {
    const shopnum = app.globalData.shoopnum;
    const settion = app.globalData.session;
    const vip = this.data.vipdetail;  //vip详情
    var glist = this.data.goodList;
    var goodData = [];
    for (var i = 0; i < glist.length; i++) {
      var templist = glist[i];
      var td = { 'good_bn': templist.goodbtn, 'num': '1' };
      goodData.push(td);
    }
    var memberid = vip.member_id?vip.member_id:'';
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'order.decreate', "seller_bn": shopnum, 'session_id': settion, "goods_data": goodData, 'member_id': memberid,
    }, (err, res) => {
      if (!res || res == '') {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 5000
        })
        return
      }
      var res_data = res.data;
      var oderid = res_data.order_id;
      if (oderid){
        wx.navigateTo({
          url: '/pages/content/maxpay?ordeid=' + oderid + '&vip=' + vip.mobile,
        })
      }
     
    });
  },
  //无商品下单
  fastorder: function () {
    const shopnum = app.globalData.shoopnum;
    const settion = app.globalData.session;
    var payamount = this.data.inputnumber;
    const vip = this.data.vipdetail;  //vip详情
    var memberid = vip.member_id ? vip.member_id : '';

    var money = (parseFloat(payamount) * 100).toFixed(0);
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'order.docreate', "seller_bn": shopnum, 'session_id': settion, "pay_amount": money, 'member_id': memberid,
    }, (err, res) => {
      if (!res || res == '') {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 5000
        })
        return
      }
      var res_data = res.data;
      var oderid = res_data.order_id;
      if (!oderid)return;
      wx.navigateTo({
        url: '/pages/content/maxpay?ordeid=' + oderid + '&vip=' + vip.mobile,
      })
    });
  }
})